import os, suds, traceback, sys, subprocess, bz2, base64
sys.stdout.flush()
workqueue = suds.client.Client('http://184.73.196.191:9540/workqueue?wsdl')
crashes = suds.client.Client('http://184.73.196.191:9540/crashes?wsdl')
while True:
    packet = workqueue.service.popPacket()
    if packet == None:
        break
    try:
        for url in packet.urls:
            sys.stdout.flush()
            proc = subprocess.Popen(["wget", "-Ooutput.html", "-t 3", "-T 5", str(url)], cwd=os.getcwd(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = proc.communicate()
            code = proc.wait()
            sys.stdout.flush()
            if code == 0:
		f = open(os.getcwd() + '/output.html')
		size = os.path.getsize(os.getcwd() + '/output.html')		
		html = bz2.compress(f.read())
		print repr(code) + '\t' + str(size) + '\t' + repr(url)
		crashes.service.recordCrashDump(unicode(url),base64.b64encode(html),)
            	f.close()
            else:
                crashes.service.recordCrashDump(unicode(url),str(0), )
		print repr(code) + '\t\t' + repr(url)
            proc = subprocess.Popen(["rm", "output.html"], cwd=os.getcwd(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = proc.communicate()
            code = proc.wait()
        workqueue.service.recordPacketCompletion(packet.id)
    except:
	sys.stdout.flush()
        workqueue.service.reAddPendingPacket(packet.id)
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print "*** print_tb:"
        traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
        print "*** print_exception:"
        traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
        print "*** print_exc:"
        traceback.print_exc()
        break
print 'Done'
sys.stdout.flush()
sys.stderr.flush()
